import yagmail,yaml,logging,json

import unittest
from XTestRunner import HTMLTestRunner
# 发送邮件
def get_mail(file):
    mail=yagmail.SMTP(user='3517001032@qq.com',password='tjikoruxunlwdafa',host='smtp.qq.com')
    mail.send(to='3517001032@qq.com',subject='测试报告',contents='测试报告已生成，请查收！',attachments=file)


# 读取yaml文件
def readjson(filepath):
    with open(filepath,'r',encoding='utf-8',errors='ignore') as f:
        return yaml.safe_load(f)

# 读取json文件
def readjsons(filepath):
    with open(filepath,'r',encoding='utf-8',errors='ignore') as f:
        return json.load(f)
def assert_equal(expected,actual):
    try:
        assert expected == actual
    except AssertionError:
        logging.error(f"实际结果：{actual}，期望结果：{expected}")
        raise
def get_log():
    logging.basicConfig(filename='../log/log.txt', filemode='w+', level=logging.INFO,
                        format='%(asctime)s - %(filename)s - %(levelname)s: %(message)s')

# 运行测试用例
def job_function():
    suite = unittest.defaultTestLoader.discover(start_dir='../TestCase', pattern='Test*.py', )
    rune = HTMLTestRunner(open('report.html', 'wb'), title='测试报告', description='用例执行情况')
    rune.run(suite)
    get_mail(['../run/report.html','../log/log.txt'])


